package com.jwthhealth_pub.common;

import android.util.Log;
import android.widget.Toast;

import com.jwthhealth_pub.BuildConfig;
import com.jwthhealth_pub.app.App;

/**
 * log util
 * Created by Administrator on 2017/6/30.
 */

public class LogUtil {
    private static final String LOG_PREFIX = "jwth_";
    private static final int LOG_PREFIX_LENGTH = LOG_PREFIX.length();
    private static final int MAX_LOG_TAG_LENGTH = 23;

    public static boolean LOGGING_ENABLED = !BuildConfig.BUILD_TYPE.equalsIgnoreCase("release");

    public static String makeLogTag(String str) {
        if (str.length() > MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH) {
            return LOG_PREFIX + str.substring(0, MAX_LOG_TAG_LENGTH - LOG_PREFIX_LENGTH - 1);
        }

        return LOG_PREFIX + str;
    }

    /**
     * 混淆时不可用
     */
    public static String makeLogTag(Class cls) {
        return makeLogTag(cls.getSimpleName());
    }

    private static boolean isToast = true;
    private static boolean isLog = true;

    public static void setIsToast(boolean isToast) {
        LogUtil.isToast = isToast;
    }

    public static boolean isLog() {
        return isLog;
    }

    public static void toast(final String msg) {
        if (isToast) {
            Toast.makeText(App.mContext, msg, Toast.LENGTH_SHORT).show();
        }
    }

    public static void i(String msg, String tag) {
        if (LOGGING_ENABLED) {
            Log.i(tag, msg);
        }
    }

    public static void e(String msg, String tag) {
        if (LOGGING_ENABLED) {
            Log.e(tag, msg);
        }
    }
}
